<template>
  <div class="addshouhuodizhi">
    <div class="add1">
      <div>
        <label for="">收货人</label>
        <input type="text" placeholder="请输入收货人姓名" v-model="form.name" />
      </div>
      <div>
        <label for="">手机号码</label>
        <input type="text" placeholder="请输入手机号码" v-model="form.phone" />
      </div>
      <div @click="diqukaiguan = true">
        <label for="">所在地区</label>
        <input type="text" placeholder="请选择地址" v-model="form.diqu" />
      </div>
      <div>
        <label for="">详细地址</label>
        <input type="text" placeholder="请输入详细地址" v-model="form.dizhi" />
      </div>
    </div>
    <div class="add2">
      <div class="top">
        <p>标签</p>
        <span>家</span>
        <span>公司</span>
        <span>学校</span>
      </div>
      <div class="bottom">
        <div class="left">
          <p>设置默认地址</p>
          <span>提醒:每次下单会默认推荐使用该地址</span>
        </div>
        <div class="right">
          <div
            class="kaiguan"
            @click="form.kaiguan = !form.kaiguan"
            :class="[form.kaiguan ? 'on' : '']"
          >
            <span></span>
          </div>
        </div>
      </div>
    </div>

    <button @click="add">保存</button>
    <transition name="chai1">
      <div class="diqu" v-show="diqukaiguan"></div>
    </transition>
    <transition name="chai2"
      ><div class="diqu2" v-show="diqukaiguan">
        <div class="x" @click="diqukaiguan = false">×</div>
        <div class="title">配送至</div>
        <div class="guojia"><span>中国大陆</span></div>
        <div class="dizhilist">
          <div v-for="item in list" :key="item.letter">
            <div class="left">{{ item.letter.toUpperCase() }}</div>
            <ul class="right">
              <li
                v-for="item2 in item.data"
                :key="item2.cityId"
                @click="(form.diqu = item2.name), (diqukaiguan = false)"
              >
                {{ item2.name }}
              </li>
            </ul>
          </div>
        </div>
      </div></transition
    >
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: null,
        phone: null,
        diqu: null,
        dizhi: null,
        kaiguan: false,
      },
      list: null,
      diqukaiguan: false,
      jiazaizhong: false,
    };
  },
  mounted() {
    this.$emit("fanhui", 5);
    this.dizhi();
    //判断修改还是新增
    this.xiugai();
  },
  methods: {
    // 保存
    add() {
      this.jiazaizhong = true;
      if (this.$route.query.dizhiid) {
        axios
          .post("http://121.89.205.189:3000/api/address/update", {
            token: sessionStorage.getItem("token"),
            userid: sessionStorage.getItem("userid"),
            name: this.form.name,
            tel: this.form.phone,
            province: this.form.diqu,
            city: this.form.diqu,
            county: this.form.diqu,
            addressDetail: this.form.dizhi,
            isDefault: this.form.kaiguan,
            addressid: this.$route.query.dizhiid.addressid,
          })
          .then((res) => {
            this.$router.push({ name: "shouhuodizhi" });
            this.jiazaizhong = false;
          });
        return;
      }
      if (this.$route.query.dingdandizhi) {
        axios
          .post("http://121.89.205.189:3000/api/order/updateOrderAddress", {
            token: sessionStorage.getItem("token"),
            userid: sessionStorage.getItem("userid"),
            name: this.form.name,
            tel: this.form.phone,
            province: this.form.diqu,
            city: this.form.diqu,
            county: this.form.diqu,
            addressDetail: this.form.dizhi,
            time: this.$route.query.time,
          })
          .then((res) => {
            this.$router.push({
              name: "dingdantianxie",
              query: {
                time: this.$route.query.time,
              },
            });
          });
        return;
      }
      axios
        .post("http://121.89.205.189:3000/api/address/add", {
          token: sessionStorage.getItem("token"),
          userid: sessionStorage.getItem("userid"),
          name: this.form.name,
          tel: this.form.phone,
          province: this.form.diqu,
          city: this.form.diqu,
          county: this.form.diqu,
          addressDetail: this.form.dizhi,
          isDefault: this.form.kaiguan,
        })
        .then((res) => {
          this.$router.push({ name: "shouhuodizhi" });
        });
    },
    //获取地址
    dizhi() {
      axios
        .get("/chaipi/city/sortCity.json")
        //    http://121.89.205.189:3000/city/city.json
        .then((res) => {
          this.list = res.data;
        });
    },
    xiugai() {
      if (this.$route.query.dizhiid) {
        this.form.name = this.$route.query.dizhiid.name;
        this.form.phone = this.$route.query.dizhiid.tel;
        this.form.diqu = this.$route.query.dizhiid.county;
        this.form.dizhi = this.$route.query.dizhiid.addressDetail;
        this.form.kaiguan = this.$route.query.dizhiid.isDefault;
      }

      //订单地址修改
      if (this.$route.query.dingdandizhi) {
        this.form.name = this.$route.query.dingdandizhi.name;
        this.form.phone = this.$route.query.dingdandizhi.tel;
        this.form.diqu = this.$route.query.dingdandizhi.county;
        this.form.dizhi = this.$route.query.dingdandizhi.addressDetail;
      }
    },
  },
};
</script>


<style lang="less" scoped>
.add1 {
  width: 100%;
  height: 3rem;
  background: white;
  padding: 0 0.15rem;
  box-sizing: border-box;
  > div {
    border-bottom: 1px solid #ccc;
    padding: 0.1rem 0;
    display: flex;
    font-size: 0.15rem;
    > label {
      padding: 0 0.05rem;
      width: 0.8rem;
    }
    > input {
      flex: 1;
      border: none;
      margin-left: 0.5rem;
    }
  }
}
.add2 {
  width: 100%;
  height: 2rem;
  background: white;
  padding: 0 0.15rem;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;

  > .top {
    font-size: 0.15rem;
    display: flex;
    > p {
      padding: 0 0.05rem;
    }
    > span {
      padding: 0 0.2rem;
      border: 1px solid #ccc;
      border-radius: 50px;
      margin-left: 0.1rem;
    }
  }
  > .bottom {
    display: flex;
    flex: 1;
    justify-content: space-between;
    .left {
      display: flex;
      flex-direction: column;
      justify-content: center;
      > span {
        color: #ccc;
      }
      > p {
        font-size: 0.16rem;
      }
    }
    .right {
      display: flex;
      flex-direction: column;
      justify-content: center;
      > .kaiguan {
        width: 0.5rem;
        height: 0.25rem;
        background: #eeeeee;
        border: 1px solid #e0e0e0;
        border-radius: 50px;
        display: flex;
        align-items: center;
        transition: 0.5s;
        > span {
          width: 0.2rem;
          height: 0.2rem;
          border-radius: 55px;
          color: white;
          background: #eeeeee;
          transition: 0.5s;
          box-shadow: 0 0.8vw 1px 0 rgb(0 0 0 / 5%),
            0 0.533333vw 0.533333vw 0 rgb(0 0 0 / 10%),
            0 0.8vw 0.8vw 0 rgb(0 0 0 / 5%);
        }
      }
    }
  }
}
.on {
  background: #f98510 !important;
  transition: 0.1s;
  > span {
    transform: translateX(0.3rem);
    transition: 0.5s;
  }
}
button {
  width: 3.5rem;
  height: 0.4rem;
  background: linear-gradient(90deg, #ffa100, #ef5b27);
  border: none;
  border-radius: 0.3rem;
  font-size: 13px;
  color: white;
  position: fixed;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%);
}
.diqu {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.707);
  position: fixed;
  top: 0;
  z-index: 999;
}
.dizhilist {
  height: 100%;
  display: flex;
  padding: 0.1rem;
  flex-direction: column;
  overflow: auto;
  // position: fixed;
  // top: 0;
  // z-index: 999;
  > div {
    display: flex;
    width: 100%;
    margin-top: 0.2rem;
    border-bottom: 1px solid #ccc;
    > .left {
      width: 0.8rem;
      font-size: 0.33rem;
    }
    > .right {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      > li {
        font-size: 0.16rem;
        padding: 0.1rem;
      }
    }
  }
}
.diqu2 {
  width: 100%;
  height: 500px;
  position: fixed;
  bottom: 0;
  background: white;
  border-radius: 15px 15px 0 0;
  z-index: 9999;
  .x {
    position: absolute;
    top: 10px;
    right: 10px;
    font-size: 0.33rem;
  }
  > .title {
    height: 0.62rem;
    text-align: center;
    font-size: 0.2rem;
    line-height: 0.62rem;
  }
  .guojia {
    height: 0.5rem;
    line-height: 0.5rem;
    text-align: center;
    font-size: 0.18rem;
    > span {
      color: #ef5b27;
      border-bottom: 2px solid #ef5b27;
      display: inline;
    }
  }
}
/* 进入的起点/离开的终点 */
.chai1-enter,
.chai1-leave-to {
  // transform: translateX(-100%);
  opacity: 0;
}
/* 进入和离开的动画 */
.chai1-enter-active,
.chai1-leave-active {
  transition: 0.5s;
}
/* 进入的终点/离开的起点 */
.chai1-enter-to,
.chai1-leave {
  // transform: translateX(0%);
  opacity: 1;
}

//2
/* 进入的起点/离开的终点 */
.chai2-enter,
.chai2-leave-to {
  transform: translateY(100%);
  // opacity: 0;
}
/* 进入和离开的动画 */
.chai2-enter-active,
.chai2-leave-active {
  transition: 0.5s;
}
/* 进入的终点/离开的起点 */
.chai2-enter-to,
.chai2-leave {
  transform: translateY(0%);
  // opacity: 1;
}
</style>